﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace UI
{
    public class UIFieldRunMapTeamPanelInfo : MonoBehaviour
    {
        public GameObject leaderPanel;
        public GameObject memberPanel;

        public UIFieldRunMapTeamMember member;
        public UIFieldRunMapTeamLeader leader;

        public void Refresh(Team team)
        {
            if (team != null)
            {
                StaticData.playerData.team = team;
                StaticData.playerData.teamId = team.teamId;
                Init();
            }
            else
            {
                gameObject.SetActive(false);
            }
        }

        public void Init()
        {
            if (StaticData.playerData.team != null && StaticData.playerData.team.memberList.Count > 0)
                gameObject.SetActive(true);
            else
            {
                Debug.LogError("[error] team list count = 0");
                return;
            }

            if (StaticData.playerData.team.memberList[0].playerId == StaticData.playerData.playerId)
            {
                leaderPanel.SetActive(true);
                memberPanel.SetActive(false);
                leader.Init();
            }
            else
            {
                memberPanel.SetActive(true);
                leaderPanel.SetActive(false);
                member.Init();
            }
        }

        /// <summary>
        /// 主动离开队伍
        /// </summary>
        public void OnExitTeam()
        {
            var str = TableTextBlo.GetText("field_leave_team");
            BoxManager.CreateMessageBoxWithCofirmAndCancelButton(str, true, (b) => 
            {
                if(b)
                {
                    var room = Room.RoomManager.currentRoom as Room.RoomField;
                    var net = room.netManager as Room.RoomNetField;
                    if (net != null)
                    {
                        net.SendCSExitTeam();
                    }

                    Closed();
                }
            },TableTextBlo.GetText("union_leave_team"));
           
        }
        
        public void Closed()
        {
            if(member.gameObject.activeSelf)
                member.Exit();
            if (leader.gameObject.activeSelf)
                leader.Exit();
            gameObject.SetActive(false);
        }

    }
}